package com.macrosoftinc.feedreaderxtreme.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.macrosoft.frpd.parser.helper.TorrentFeedBO;
import com.macrosoftinc.feedreaderxtreme.R;

public class CustomTorrentsListAdapter extends ArrayAdapter<TorrentFeedBO> {

	private Context mContext;
	private int mLayoutId;

	private LinearLayout progressBarDialog = null;
	List<TorrentFeedBO> items;

	/**
	 * Constructor
	 */

	public CustomTorrentsListAdapter(Context context, int layoutId, int textViewResourceId, List<TorrentFeedBO> items) {
		super(context, textViewResourceId, items);
		mContext = context;
		mLayoutId = layoutId;
		this.items = items;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return items.size();
	}

	/**
	 * @return the items
	 */
	public List<TorrentFeedBO> getItems() {
		return items;
	}

	/**
	 * @param items
	 *            the items to set
	 */
	public void setItems(List<TorrentFeedBO> items) {
//		if(this.items == null){
			this.items = items;
//		}else{
//			this.items.addAll(items);
//		}
		
	}

	/**
	 * getView
	 * 
	 * Return a view that displays an item in the array.
	 * 
	 */

	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(mLayoutId, null);
		}

		View itemView = v;
		TorrentFeedBO torrentItem = getItem(position);
		TextView title = (TextView) itemView.findViewById(R.id.torrent_title);
		if (title != null)
			title.setText(torrentItem.title);
		TextView date = (TextView) itemView.findViewById(R.id.torrent_date);
		if (date != null)
			date.setText(torrentItem.pubDate);
		TextView size = (TextView) itemView.findViewById(R.id.torrent_size_value);
		if (size != null)
			size.setText(torrentItem.sizeUnit);
		TextView seeders = (TextView) itemView.findViewById(R.id.torrent_seeders_value);
		if (seeders != null)
			seeders.setText(torrentItem.seeds);
		TextView leechers = (TextView) itemView.findViewById(R.id.torrent_leechers_value);
		if (leechers != null)
			leechers.setText(torrentItem.leachers);

		View toolbar = v.findViewById(R.id.toolbar);
		((LinearLayout.LayoutParams) toolbar.getLayoutParams()).bottomMargin = -50;
		toolbar.setVisibility(View.GONE);

		return itemView;
	}
} // end class CustomAdapter
